package com.woniuxy.house.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.entity.broker.Broker;
import com.woniuxy.entity.house.Renthouse;
import com.woniuxy.entity.house.dto.RenthouseDto;
import com.woniuxy.entity.house.dto.RenthouseQO;
import com.woniuxy.house.feign.BrokerService;
import com.woniuxy.house.mapper.RenthouseMapper;
import com.woniuxy.house.service.RenthouseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liangyu
 * @since 2021-03-19
 */
@Service
@Slf4j
public class RenthouseServiceImpl extends ServiceImpl<RenthouseMapper, Renthouse> implements RenthouseService {

    @Resource
    private RenthouseMapper renthouseMapper;

    @Resource
    private BrokerService brokerService;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    // 分页查询所有租房
    @Override
    public Page<Renthouse> findPage(int current,int size) {
        return renthouseMapper.selectPage(new Page<>(current,size),null);
    }

    // 通过租房id查询到户型信息，配套信息，楼盘信息，对应三个经纪人名称，头像
    @Override
    public RenthouseDto findOneById(int id) {
        RenthouseDto renthouseDto;
        if ((renthouseDto = renthouseMapper.findOneById(id)) != null) {
            // 根据id查询获取认证经纪人
            Broker cbBroker = brokerService.getSingleBroker(renthouseDto.getCbid()).getData();
            // 根据id查询获取维护经纪人
            Broker mbBroker = brokerService.getSingleBroker(renthouseDto.getMbid()).getData();
            // 根据id查询获取实勘经纪人
            Broker ibBroker = brokerService.getSingleBroker(renthouseDto.getIbid()).getData();

            renthouseDto.setCbName(cbBroker.getName()).setCbImage(cbBroker.getAvatar());
            renthouseDto.setMbName(mbBroker.getName()).setMbImage(mbBroker.getAvatar());
            renthouseDto.setIbName(ibBroker.getName()).setIbImage(ibBroker.getAvatar());
        }
        return renthouseDto;
    }

    // 根据租房id软删除租房信息，将status修改为1
    @Override
    public boolean deleteOneById(int id) {
        return renthouseMapper.deleteOneById(id) > 0;
    }

    // 根据租房id修改租房信息
    @Override
    public boolean updateByRid(Renthouse renthouse) {
        UpdateWrapper<Renthouse> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",renthouse.getId());
        return renthouseMapper.update(renthouse, updateWrapper) > 0;
    }

    // 根据条件分页查询（包括排序）
    @Override
    public Page<RenthouseDto> findLike(int current,int size, RenthouseQO renthouseQO) {
        return renthouseMapper.findLike(new Page<>(current, size), renthouseQO);
    }

    // 预加载 将热点数据提前放入redis，例如 分页查询租房第一页数据(十个)
//    @Override
//    public void afterPropertiesSet() throws Exception {
//        // 缓存第一页十个数据
//        Page<RenthouseDto> page = renthouseMapper.findLike(new Page<>(1, 10), new RenthouseQO());
//        redisTemplate.opsForValue().set("find:com.woniuxy.house.service.impl.RenthouseServiceImplfindLike:1:10:" +
//                "RenthouseQO(minFloor=null, maxFloor=null, houseHead=null, elevator=null, " +
//                "rentway=null, minPrice=null, maxPrice=null, sortPrice=null)",page);
//    }
}
